var http=require('http');


//const renderer=require('./renderer');

var querystring = require('querystring');

const ipcRenderer=require('electron');




console.log("process:"+process.isMainFrame);


function fnLogin() {
    var oUname = document.getElementById("uname")
    var oUpass = document.getElementById("upass")
    var oError = document.getElementById("error_box")
    var isError = true;
    // if (oUname.value.length > 20 || oUname.value.length < 6) {
    //  oError.innerHTML = "用户名请输入6-20位字符";
    //  isError = false;
    //  return;
    // }else if((oUname.value.charCodeAt(0)>=48) && (oUname.value.charCodeAt(0)<=57)){
    //  oError.innerHTML = "首字符必须为字母";
    //  return;
    // }else for(var i=0;i<oUname.value.charCodeAt(i);i++){
    //  if((oUname.value.charCodeAt(i)<48)||(oUname.value.charCodeAt(i)>57) && (oUname.value.charCodeAt(i)<97)||(oUname.value.charCodeAt(i)>122)){
    //   oError.innerHTML = "必须为字母跟数字组成";
    //   return;
    //  }
    // }
    
    // if (oUpass.value.length > 20 || oUpass.value.length < 6) {
    //  oError.innerHTML = "密码请输入6-20位字符"
    //  isError = false;
    //  return;
    // }




   

  //  window.alert("登录成功")



   getCompanyName(oUname.value,oUpass.value);




   }


function getCompanyName(username,password){


    var oError = document.getElementById("error_box");



    http.get("http://www2.boohersmart.com:3000/api/admin/login/"+username+"/"+password,function(req){

        var html='';  

        req.on('data', function (chunk) {  
            console.log('BODY: ' + chunk);  
        //JSON.parse(chunk)
        html+=chunk;
        });
        
        req.on('end',function (){

            console.log('html: ' + html);  

           var r=JSON.parse(html);
            var x=r.data[0];

            if(x==null){

               oError.innerHTML="账号或密码有误";
            }else{

                console.log("gid:"+x.id);

             //   renderer.getDeviceId(x.gid)

          //   sendgid(x.gid);
           

       //     console.log("gid:"+x);
             //  renderer.getDeviceId(x.gid);

             if(x.gid==8){

                oError.innerHTML="无操作权限";
             }else{
                Toindex(x.id,x.gid);

             }

      
            }


        })



     


      req.on('error', function (e) {  
        console.log('problem with request: ' + e.message);  
    });  
      

      
      });


}



function Toindex(a_id,g_id){

    const param={
        aid:a_id,
        gid:g_id
    
    }

    const params=JSON.stringify(param);

    window.location.href="index.html";

    localStorage.setItem('params',params);
}

  



 




